﻿using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamPlace_SpecialityKinds_Option : BaseForm
    {
        TB_Sys_ExamPlaceSpecialityKindsMapping model = null;
        ServiceBase service = new ServiceBase();
        string SpecialityKindsNO = string.Empty;
        string ExamPlaceNO = string.Empty;
        string ExamPlaceSpecialityKindsMappingID = string.Empty;
        public Base_ExamPlace_SpecialityKinds_Option(string strExamPlaceSpecialityKindsMappingID, string strExamPlaceNO, string strSpecialityKindsNO)
        {
            InitializeComponent();
            ExamPlaceSpecialityKindsMappingID = strExamPlaceSpecialityKindsMappingID;
            ExamPlaceNO = strExamPlaceNO;
            SpecialityKindsNO = strSpecialityKindsNO;
            BindExamPlace();
            BindSpec();
            BindExamPlaceSpec();
        }

        private void BindExamPlaceSpec()
        {
            if (ExamPlaceSpecialityKindsMappingID.Length > 0)
            {
                model = service.GetExamPlaceSpecialityKindsMappingModel(ExamPlaceSpecialityKindsMappingID);
                if (model != null)
                {
                    model.Attach();
                    txtExamStartDate.Text = model.ExamStartDate.DateTimeToyyyy_MM_dd();
                    txtExamEndDate.Text = model.ExamEndDate.DateTimeToyyyy_MM_dd();
                    txtSceneEndDate.Text = model.SceneEndDate.DateTimeToyyyy_MM_dd();
                    txtSceneStartDate.Text = model.SceneStartDate.DateTimeToyyyy_MM_dd();
                    if (model.IsOpen == "1")
                    {
                        chkIsOpen.Checked = true;
                    }
                    else
                    {
                        chkIsOpen.Checked = false;
                    }
                    if (model.IsOnlineReg == "1")
                    {
                        chkIsOnileReg.Checked = true;
                    }
                    else
                    {
                        chkIsOnileReg.Checked = false;
                    }
                }
            }
        }

        private void BindExamPlace()
        {
            if (ExamPlaceNO.Length > 0)
            {
                TB_Sys_ExamPlace modelPlace = service.GetSysExamPlaceModel(ExamPlaceNO);
                if (modelPlace != null)
                {
                    txtPlaceNo.Text = modelPlace.ExamPlaceNO;
                    txtPlaceName.Text = modelPlace.ExamPlaceName;
                    txtPlaceNo.Enabled = txtPlaceName.Enabled = false;
                }
            }
        }

        private void BindSpec()
        {
            if (SpecialityKindsNO.Length > 0)
            {
                TB_Sys_SpecialityKinds modelSpec = service.GetSpecialityKindsModel(SpecialityKindsNO);
                if (modelSpec != null)
                {
                    txt_SpecialityKindsID.Text = modelSpec.SpecialityKindsID;
                    txt_SpecialityKindsNO.Text = modelSpec.SpecialityKindsNO;
                    txt_SpecialityKindsName.Text = modelSpec.SpecialityKindsName;
                    txt_Sort.Text = modelSpec.Sort.ToSafeString();
                    if (modelSpec.IsActive == "1")
                    {
                        chkSFYX.Checked = true;
                    }
                    else
                    {
                        chkSFYX.Checked = false;
                    }
                    if (modelSpec.IsSchool == "1")
                    {
                        chkIsXK.Checked = true;
                    }
                    else
                    {
                        chkIsXK.Checked = false;
                    }
                    txt_SpecialityKindsID.Enabled = txt_SpecialityKindsName.Enabled = txt_SpecialityKindsNO.Enabled = txt_Sort.Enabled =
                        chkIsXK.Enabled = chkSFYX.Enabled = false;
                }
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtSceneStartDate.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请选择现考试开始时间！");
                txtSceneStartDate.Focus();
                return;
            }
            if (txtSceneEndDate.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请选择考试结束时间！");
                txtSceneEndDate.Focus();
                return;
            }
            if (txtExamStartDate.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请选择现场报名开始时间！");
                txtExamStartDate.Focus();
                return;
            }
            if (txtExamEndDate.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请选择现场报名结束时间！");
                txtExamEndDate.Focus();
                return;
            }
            if (model == null)
            {
                model = new TB_Sys_ExamPlaceSpecialityKindsMapping();
            }
            model.ExamEndDate = txtExamEndDate.Text.ToSafeDateTime();
            model.ExamPlaceNO = ExamPlaceNO;
            model.ExamStartDate = txtExamStartDate.Text.ToSafeDateTime();
            model.ExamYear = SessionerHelper.SystemYear;
            if (chkIsOnileReg.Checked)
            {
                model.IsOnlineReg = "1";
            }
            else
            {
                model.IsOnlineReg = "0";
            }
            if (chkIsOpen.Checked)
            {
                model.IsOpen = "1";
            }
            else
            {
                model.IsOpen = "0";
            }
            model.SceneEndDate = txtSceneEndDate.Text.ToSafeDateTime();
            model.SceneStartDate = txtSceneStartDate.Text.ToSafeDateTime();
            model.SpecialityKindsNO = SpecialityKindsNO;

            if (service.SaveExamPlaceSpecialityKindsMapping(model))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
